import 'package:flutter/material.dart';

/// 屏幕自适应工具类
class SizeUtil {
  static late MediaQueryData _mediaQueryData;
  static late double screenWidth;
  static late double screenHeight;
  static late double defaultSize;
  static late Orientation orientation;

  // UI设计图的尺寸
  static const double designWidth = 1280.0;
  static const double designHeight = 800.0;

  void init(BuildContext context) {
    _mediaQueryData = MediaQuery.of(context);
    screenWidth = _mediaQueryData.size.width;
    screenHeight = _mediaQueryData.size.height;
    orientation = _mediaQueryData.orientation;
    
    // 根据屏幕宽度计算基准尺寸
    defaultSize = screenWidth / designWidth;
  }

  // 根据UI设计图的尺寸计算实际尺寸
  static double setWidth(double width) {
    return width * defaultSize;
  }

  static double setHeight(double height) {
    return height * defaultSize;
  }

  // 字体大小适配方法
  static double setSp(double size) {
    return size * defaultSize;
  }
}

/// 扩展num类型，方便使用
extension SizeExtension on num {
  double get w => SizeUtil.setWidth(this.toDouble());
  double get h => SizeUtil.setHeight(this.toDouble());
  double get sp => SizeUtil.setSp(this.toDouble());
}